import {reqAdressInfo,reqShopInfo,reqSubmitOrder} from '@/api'

let state ={
    // 用户地址信息
    address:[],
    // 商品清单数据
    tradeInfo:{},
    // 订单Id
    payId:''
};
let mutations = {
    GETADDRESS(state,address){
        state.address = address
    },
    GETSHOPINFO(state,tradeInfo){
        state.tradeInfo = tradeInfo
    },

    SUBMITINFO(state,payId){
        state.payId = payId
    }
};
let actions = {

    // 获取用户地址信息
   async getAddress({commit,dispatch,state}){
        let result = await reqAdressInfo()

        if(result.code == 200){
            commit('GETADDRESS',result.data)
            return 'ok'
        }else{
            return Promise.reject(new Error(result.message))
        }

    },


    // 获取商品清单的数据
    async getShopInfo({commit,dispatch,state}){
        let result = await reqShopInfo()

        if(result.code == 200){
            commit('GETSHOPINFO',result.data)
            return 'ok'
        }else{
            return Promise.reject(new Error(result.message))
        }


    },


    // 提交订单
    async submitInfo({commit,dispatch,state},{tradeNo,data}){
        let result = await reqSubmitOrder(tradeNo,data)

        if(result.code == 200){
            commit('SUBMITINFO',result.data)
            return 'ok'
        }else{
            return Promise.reject(new Error(result.message))
        }


    }
};
let getters ={};

export default {
    state,
    mutations,
    actions,
    getters
}